﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Forms.Design
{

    /// <summary>
    /// 集合编辑器的设计界面。
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public partial class CollectionEditorForm<T> : Marmat.Forms.Form
    {
        internal ICollection<T> sourceCollection;
        /// <summary>
        /// 内部集合
        /// </summary>
        protected List<T> _collection = new List<T>();

        /// <summary>
        /// 构建集合编辑器的设计界面实例。
        /// </summary>
        protected CollectionEditorForm() : this(new List<T>())
        {
        }

        /// <summary>
        /// 构建集合编辑器的设计界面实例。
        /// </summary>
        /// <param name="source"></param>
        public CollectionEditorForm(ICollection<T> source)
        {
            InitializeComponent();
            this.sourceCollection = source;
            this._collection.AddRange(source.ToArray());

            foreach (var item in this._collection)
            {
                var node = this.treeList1.Nodes.Add();
                node.Tag = item;
            }
            if (source.Count > 0)
                this.propertyGridControl1.SelectedObject = this.treeList1.FocusedNode.Tag;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button3_Click(object sender, EventArgs e)
        {
            T item = this.NewItem(sender, e);
            if (item != null)
            {
                this.AddItem(item);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button4_Click(object sender, EventArgs e)
        {
            var node = this.treeList1.FocusedNode;
            if (node != null && node.Tag is T item)
            {
                this._collection.Remove(item);
                this.treeList1.Nodes.Remove(node);
            }
        }

        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button5_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要清空集合的所有列表吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                this._collection.Clear();
                this.treeList1.Nodes.Clear();
                this.propertyGridControl1.SelectedObject = null;
            }
        }

        /// <summary>
        /// 上移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button6_Click(object sender, EventArgs e)
        {
            var node = this.treeList1.FocusedNode;
            if (node != null && node.Tag is T item)
            {
                int index = this._collection.IndexOf(item);
                this._collection.Remove(item);
                this._collection.Insert(Math.Max(0, index - 1), item);
                this.treeList1.SetNodeIndex(node, index - 1);
                this.treeList1.FocusedNode = node;
            }
        }

        /// <summary>
        /// 下移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button7_Click(object sender, EventArgs e)
        {
            var node = this.treeList1.FocusedNode;
            if (node != null && node.Tag is T item)
            {
                int index = this._collection.IndexOf(item);
                this._collection.Remove(item);
                this._collection.Insert(Math.Min(this._collection.Count, index + 1), item);
                this.treeList1.SetNodeIndex(node, index + 1);
                this.treeList1.FocusedNode = node;
            }
        }

        /// <summary>
        /// 添加项
        /// </summary>
        /// <param name="item"></param>
        public void AddItem(T item)
        {
            this._collection.Add(item);
            var node = this.treeList1.Nodes.Add();
            node.Tag = item;
            this.treeList1.FocusedNode = node;
            this.propertyGridControl1.SelectedObject = item;
        }

        /// <summary>
        /// 添加新项时执行。
        /// </summary>
        /// <returns></returns>
        protected virtual T NewItem(object sender, EventArgs e)
        {
            return (T)typeof(T).Assembly.CreateInstance(typeof(T).FullName);
        }


        private void TreeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            this.propertyGridControl1.SelectedObject = e.Node?.Tag;
        }

        private void TreeList1_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
        {
            e.CellText = e.Node.Tag.ToString();
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender, EventArgs e)
        {
            ////移除已经删除的字段
            //foreach (var item in this.sourceCollection)
            //{
            //    if (!this._collection.Contains(item))
            //    {
            //        this.sourceCollection.Remove(item);
            //    }
            //}

            //int index = 0;
            //foreach (var item in this._collection)
            //{
            //    if (!this.sourceCollection.Contains(item))
            //    {
            //        this.sourceCollection.Insert(index, item);
            //    }
            //    else if (this.sourceCollection.IndexOf(item) != index)
            //    {
            //        this.sourceCollection.Remove(item);
            //        this.sourceCollection.Insert(index, item);
            //    }
            //    index++;
            //}
            this.sourceCollection.Clear();
            foreach (var item in this._collection)
            {
                this.sourceCollection.Add(item);
            }
            this.DialogResult = DialogResult.OK;
        }

        private void treeList1_SelectionChanged(object sender, EventArgs e)
        {
            var nodes = new List<object>();
            foreach (var node in this.treeList1.Selection)
            {
                if (!nodes.Contains(node?.Tag))
                    nodes.Add(node?.Tag);
            }
            this.propertyGridControl1.SelectedObjects = nodes.ToArray();
        }
    }
}
